﻿@using CQIE.TES.Model
@using CQIE.TES.WEB.USER.Models

<select name="selectEvaD">
    <option value="">选择评测</option>
    @{
        foreach (EvaluaHeight item in ViewBag.DEvaList)
        {
            <option value="@item.ID">@item.Name</option>
        }
    }
</select>


<select name="selectTeacherD">
    <option value="">选择教师</option>
    @{
        foreach (Teacher item in ViewBag.TeacherList)
        {
            <option value="@item.ID">@item.Name</option>
        }
    }
</select>
<button class="layui-btn layui-btn-normal" onclick="MakeSureD()">确定</button>

<form id="myFormDepartment" style="display: none;" class="layui-form" lay-filter="myFormDepartment">
    @{
        foreach (Question_Option item in ViewBag.Questions)
        {
            <div class="layui-form-item">
                <div class="question">
                    @item.Info
                </div>
                <div>
                    <ul class="que" id="que">
                        @foreach (Option o in item.Options)
                        {
                            <li class="frm">
                                <input type="radio" name="@(item.ID)" value="@o.Score" title="@o.Content">
                            </li>
                        }
                    </ul>
                </div>
            </div>
        }
    }
    <Button lay-submit lay-filter="myformSubD" class="layui-btn layui-btn-normal btnD">提交</Button>
</form>

<script>
    var MakeSureD;
    layui.use(['element', 'layer', 'jquery', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;

        // 确定按钮
        MakeSureD = function () {
            
            let evaId = $("[name='selectEvaD']").val();
            let tid = $("[name='selectTeacherD']").val();
            // 验证是否为空
            //            console.log("EVA", evaId);
            if (evaId === "") {
                layer.msg('请选择评教', { icon: 2 });
                return;
            }
            if (tid === "") {
                layer.msg('请选择评教教师', { icon: 2 });
                return;
            }
            $('#myFormDepartment').css('display', 'block');
            // 清空选项
            $('#myFormDepartment')[0].reset();
            var index = layer.load();
            // 保存
            sessionStorage.setItem('evaId', evaId);
            sessionStorage.setItem('tid', tid);
            $.get("/api/eva/departmentisrecorded",
                {
                    evaId: sessionStorage.getItem('evaId'),
                    teacherId: sessionStorage.getItem('uid'),
                    aimTeacherId: sessionStorage.getItem("tid")
                },
                function (res) {
                    if (res != null) {
                        // 评教已完成 加入已选项、取消提交按钮显示
                        console.log(res);
                        let obj = JSON.parse(res);
                        let data = {}
                        for (let i = 0; i < obj.length; i++) {
                            data[obj[i].qid] = obj[i].aid;
                        }
                        form.val("myFormDepartment", data);
                        $('.btnD').css('display', 'none');
                        form.render();
                    } else {
                        // 评教未完成
                        $('.btnD').css('display', 'block');
                        // 清空表单
                        $('#myFormDepartment')[0].reset();
                    }
                    layer.close(index);
                });
        }

        // 表单提交
        form.on('submit(myformSubD)',
            function () {
                var data = form.val('myFormDepartment');
                // 判断是否完成所有评教
                
                if (Object.keys(data).length !== @ViewBag.DepartmentCount) {
                    layer.msg("请完成所有评教", { icon: 2 });
                    return false;
                }
                // 提交
                var index = layer.load();
                $.post('/api/eva/department/submit',
                    {
                        evaId: sessionStorage.getItem('evaId'),
                        teacherId: sessionStorage.getItem('uid'),
                        aimTeacherId: sessionStorage.getItem("tid"),
                        answerIds: Object.values(data),
                        questionIds: Object.keys(data)
                    },
                    function (res) {
                        layer.close(index);
                        if (res) {
                            layer.msg("提交成功！", { icon: 1 });
                            $('#myFormDepartment')[0].reset();
                        } else {
                            layer.msg("失败了~好像出了一些问题！", { icon: 2 });
                        }

                    });
                // 滑动到顶部
                document.body.scrollTop = document.documentElement.scrollTop = 0;
                return false;
            });

    });


</script>